<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 人员管理逻辑
 */
class Person extends AdminBase
{
    
    /**
     * 获取人员列表
     */
    public function getPersonList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelPerson->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 人员信息编辑
     */
    public function personEdit($data = [])
    {
        
        $validate_result = $this->validatePerson->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatePerson->getError()];
        }
        
        $url = url('personList');
        
        $result = $this->modelPerson->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '人员' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelPerson->getError()];
    }

    /**
     * 获取人员信息
     */
    public function getPersonInfo($where = [], $field = true)
    {
        
        return $this->modelPerson->getInfo($where, $field);
    }
    
    /**
     * 人员删除
     */
    public function personDel($where = [])
    {
        
        $result = $this->modelPerson->deleteInfo($where);
        
        $result && action_log('删除', '人员删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelPerson->getError()];
    }
     /**
     * 导出人员列表
     */
   
    
    public function exportPersonList($where = [], $field = true, $order = '')
    {
        
        $list = $this->getPersonList($where, $field, $order, false);
        
        $titles = "名称,年龄,注册时间";
        $keys   = "name,age,create_time";
        
        action_log('导出', '导出人员列表');
        
        export_excel($titles, $keys, $list, '人员列表');
    }
    
    /**
     * 获取人员列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['p.name'] = ['like', '%'.$data['search_data'].'%'];
        
        if (!is_administrator()) {
            
            $person = session('person_info');
            
            if ($person['is_share_person']) {
                
                $ids = $this->getInheritPersonIds(PERSON_ID);
                
                $ids[] = PERSON_ID;
                
                $where['p.id'] = ['in', $ids];
                
            } else {
                
                $where['p.id'] = PERSON_ID;
            }
        }
        
        return $where;
    }
    
}
